package mapper;

import db.Token;
import mysql.JDBCUtil;
import result.CodeMsg;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * @author 计算机系 ITAEM 摆渡人
 * @description token表连接
 * @date 2022/9/20 10:00
 */
public class TokenMapper extends JDBCUtil {

    /**
     * 插入token信息
     * @param token token
     * @param username username
     * @return CodeMsg
     */
    public CodeMsg insert(String token, String username){
        String insertSQL = "INSERT INTO token VALUES (NULL,?,?,?)";
        PreparedStatement statement = getStatement(insertSQL);
        try {
            statement.setObject(1,token);
            statement.setObject(2,username);
            statement.setObject(3, LocalDateTime.now());
            statement.executeUpdate();
            return CodeMsg.success("添加成功");
        } catch (SQLException sqlException){
            System.out.println("");
            sqlException.printStackTrace();
            return CodeMsg.SERVICE_ERROR;
        } finally {
            closeAll();
        }
    }

    /**
     * 根据token获取信息
     * @param token token
     * @return ResultSet
     */
    public Token select(String token){
        String selectSQL = "SELECT * FROM token WHERE token = ?";
        PreparedStatement statement = getStatement(selectSQL);
        try {
            statement.setObject(1,token);
            ResultSet resultSet = statement.executeQuery();
            if(resultSet.next()){
                Token result = new Token();
                result.setId(resultSet.getInt("id"));
                result.setToken(resultSet.getString("token"));
                result.setUsername(resultSet.getString("username"));
                result.setCreateTime(resultSet.getTimestamp("create_time").toLocalDateTime());
                return result;
            } else {
                return null;
            }
        } catch (SQLException sqlException){
            System.out.println("服务器错误");
            sqlException.printStackTrace();
            return null;
        } finally {
            closeAll();
        }
    }

    /**
     * 根据token删除信息
     * @param token token
     * @return boolean
     */
    public boolean deleteByToken(String token){
        String deleteSQL = "DELETE FROM token WHERE token = ?";
        PreparedStatement statement = getStatement(deleteSQL);
        try {
            statement.setObject(1,token);
            return statement.execute();
        } catch (SQLException sqlException){
            System.out.println("服务器错误");
            sqlException.printStackTrace();
            return false;
        } finally {
            closeAll();
        }
    }

    /**
     * 根据username删除token信息
     * @param username username
     * @return boolean
     */
    public boolean deleteByUsername(String username){
        String deleteSQL = "DELETE FROM token WHERE username = ?";
        PreparedStatement statement = getStatement(deleteSQL);
        try {
            statement.setObject(1,username);
            return statement.execute();
        } catch (SQLException sqlException){
            System.out.println("服务器错误");
            sqlException.printStackTrace();
            return false;
        } finally {
            closeAll();
        }
    }
}
